<?php

function nearbyrnb($lng, $lat){
	$m=M();
	$sql="select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(".$lat."-latitude)/360),2)+COS(PI()*".$lat."/180)* COS(latitude * PI()/180)*POW(SIN(PI()*(".$lng."-longitude)/360),2)))) as juli from `cms_rnb` order by juli asc limit 0,10";
	$rnblist = $m->query($sql);
	if($rnblist == null) return null;
	
	$rnbs = array();
	foreach ($rnblist as $rnb){
		$juli=floor($rnb['juli']*10)/10;
		$rnba=array($rnb['property_name'].'
'.$rnb['venue_name'].'
距离'.$juli.'km', '', $rnb['thumbnail'], C('site_url').U('Wap/Booking/rnb', array('id'=>$rnb['attractionid'])));
		array_push($rnbs, $rnba);
	}
	
	return $rnbs;
}

	function nearbyrnb_previous($lng, $lat){
		$hotels = nearbyhotel($lng, $lat);
		if(null == $hotels || count($hotels) == 0){
			return null;
		}
		$rnbs = array();
		
		foreach ($hotels as $hotel){
			$url_get='https://api.spgrestaurantsandbars.com/venue?access_token=3409c85e1235609001610c2afb07486fbcbcddd1&lang_id=cn&id_property='.$hotel['hotelCode'];
			
			$result = file_get_contents($url_get);
			$decode = json_decode($result,true);

			Log::write("附近Rnb结果：".$result,LOG::INFO);
			
			foreach ($decode as $record){
				$rnb['starlink']=$record['starlink'];
				$rnb['property_name']=$record['property_name'];
				$rnb['venue_name']=$record['venue_name'];
				$rnb['distance']=$hotel['distance'];
				$rnb['img']=$record['thumbnail'];
				array_push($rnbs, $rnb);
			}
			if(count($rnbs) > 0){
				return $rnbs;
			}
		}
		return $rnbs;
	}

	function nearbyhotel($lng, $lat){
		$url_get='https://stg-api.starwoodhotels.com/property/search/geo?apiKey=95E3893EDDC243FE&lat='.$lat.'&long='.$lng.'&locale=zh_CN';

		Log::write("查询附近酒店:\r\n".$url_get, LOG::INFO);
		
		$result = file_get_contents($url_get);
		$decode = json_decode($result,true);

		Log::write("附近酒店结果:\r\n".$result, LOG::INFO);
		
		if($decode['propertySearchResponse']['status']['statusCode'] != '200')  {
			return  null;
		}
		$hotels=array();
		foreach ($decode['propertySearchResponse']['properties']['property'] as $prop){
			$hotel['img']='http://m.starwoodhotels.com'.$prop['thumbnailImage'];
			$hotel['name']=$prop['hotelName'];
			$hotel['addr']=$prop['countryName'].$prop['city'].$prop['address']['line'][0];
			$distance=floatval($prop['relativePosition']['distance']);
			$dis=round($distance, 2);
			$hotel['distance']=$dis;
			$hotel['hotelCode']=$prop['hotelCode'];
			array_push($hotels, $hotel);
		}
		Log::write("从附近酒店查询方法返回:\r\n", LOG::INFO);
		
		return $hotels;
	}
	
?>